package com.adopapa.nezhacloud.authc.controller;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.adopapa.nezhacloud.authc.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.adopapa.nezhacloud.authc.service.ResourceInfoService;
import com.adopapa.nezhacloud.authc.service.ResourcePermService;
import com.adopapa.nezhacloud.authc.service.RoleInfoService;
import com.adopapa.nezhacloud.authc.service.RolePermService;
import com.adopapa.nezhacloud.authc.service.SystemDataService;
import com.adopapa.nezhacloud.authc.service.UserInfoService;
import com.adopapa.nezhacloud.common.RestResponse;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.codegen.SqlGen;
import com.adopapa.nezhacloud.ddd.controller.BaseController;

@RestController
@RequestMapping("/authc/permission")
public class PermissionController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RoleInfoService roleInfoService;
    @Autowired
    private ResourceInfoService resourceInfoService;
    @Autowired
    private RolePermService rolePermService;
    @Autowired
    private ResourcePermService resourcePermService;
    @Autowired
    private SystemDataService systemDataService;


    @SqlGen(name = {"权限管理", "授权管理", "查询授权角色"}, method = SqlGen.METHOD_GET, path = "/roleperms")
    @GetMapping(path = "/roleperms/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse getRolePerms(@RequestHeader String authorization, @PathVariable String userId) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);

        List<RolePerm> rolePerms = rolePermService.getRolePermByUserInfo(userInfo);
        List<RoleInfo> roleInfos = rolePerms.stream().map(RolePerm::getRoleInfo).collect(Collectors.toList());

        return RestResponse.ok(roleInfos);
    }

    @SqlGen(name = {"权限管理", "授权管理", "授权角色"}, method = SqlGen.METHOD_POST, path = "/roleperms")
    @PostMapping(path = "/roleperms", produces = MediaType.APPLICATION_JSON_VALUE, //
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse saveRolePerm(@RequestHeader String authorization, //
                                     @RequestBody Map<String, Object> params) {

        String userId = getMapValue(params, "userId", String.class);
        @SuppressWarnings("unchecked")
        List<String> roleIds = getMapValue(params, "roleIds", List.class);

        if (Tools.empty(userId)) {
            throw new RuntimeException("请选择用户");
        }
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        if (Tools.empty(userInfo)) {
            throw new RuntimeException("请选择用户");
        }
        if (Tools.empty(roleIds)) {
            throw new RuntimeException("请选择角色");
        }
        List<RoleInfo> roleInfos = new LinkedList<RoleInfo>();

        roleIds.forEach(roleId -> {
            RoleInfo roleInfo = roleInfoService.getRoleInfoById(roleId);
            if (Tools.empty(roleInfo)) {
                throw new RuntimeException("请选择角色");
            }
            roleInfos.add(roleInfo);
        });

        systemDataService.selectRoles(userInfo, roleInfos);

        return RestResponse.ok();
    }

    @SqlGen(name = {"权限管理", "授权管理", "查询授权资源"}, method = SqlGen.METHOD_GET, path = "/roleperms")
    @GetMapping(path = "/resourceperms/{roleId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse getResourcePerms(@RequestHeader String authorization, @PathVariable String roleId) {
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId(roleId);

        List<ResourcePerm> resourcePerms = resourcePermService.getResourcePermByRoleInfo(roleInfo);
        List<ResourceInfo> resourceInfos = resourcePerms.stream().map(ResourcePerm::getResourceInfo).collect(Collectors.toList());

        return RestResponse.ok(resourceInfos);
    }

    @SqlGen(name = {"权限管理", "授权管理", "授权资源"}, method = SqlGen.METHOD_POST, path = "/resourceperms")
    @PostMapping(path = "/resourceperms", produces = MediaType.APPLICATION_JSON_VALUE, //
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse saveResourcePerm(@RequestHeader String authorization, //
                                         @RequestBody Map<String, Object> params) {

        String roleId = getMapValue(params, "roleId", String.class);
        @SuppressWarnings("unchecked")
        List<String> resourceIds = getMapValue(params, "resourceIds", List.class);

        if (Tools.empty(roleId)) {
            throw new RuntimeException("请选择角色");
        }
        RoleInfo roleInfo = roleInfoService.getRoleInfoById(roleId);
        if (Tools.empty(roleInfo)) {
            throw new RuntimeException("请选择角色");
        }
        if (Tools.empty(resourceIds)) {
            throw new RuntimeException("请选择资源");
        }
        List<ResourceInfo> resourceInfos = new LinkedList<ResourceInfo>();

        resourceIds.forEach(resourceId -> {
            ResourceInfo resourceInfo = resourceInfoService.getResourceInfoById(resourceId);
            if (Tools.empty(resourceInfo)) {
                throw new RuntimeException("请选择资源");
            }
            resourceInfos.add(resourceInfo);
        });

        systemDataService.selectResources(roleInfo, resourceInfos);

        return RestResponse.ok();
    }

}
